package com.sgcc.sx.pmos.e_sx_home.activity;


import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.sgcc.sx.pmos.arouter.home.HomeArouterPath;
import com.sgcc.sx.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.sx.pmos.common.base.BaseActivity;
import com.sgcc.sx.pmos.common.utils.ImmersionBarUtils;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_home.fragment.UsertTypeHistoryFregment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author rjc
 * @describe 用户身份切换页面
 */

//@Route(path = HomeArouterPath.HOME_USERTYPEHIS)
public class UserTypeListActivity extends BaseActivity {
    private SlidingTabLayout mStlContent;
    private ViewPager mViewpager;
    private final List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;
    UsertTypeHistoryFregment usertTypeHistoryFregment = new UsertTypeHistoryFregment();
//    UsertTypeListFregment usertTypeListFregment = new UsertTypeListFregment();
    private TextView tv_gotochange;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    TimePickerView pvTime;
    private String selecttime;
    int year = Calendar.getInstance().get(Calendar.YEAR);
    int month = 1;
    @Autowired
    public String userType;

    private TitleBar mTitleBar;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_usertype_list;
    }

    @Override
    protected void initViews() {
        super.initViews();



        mTitleBar = (TitleBar) findViewById(R.id.title_bar);

        mTitleBar.setTitle("身份转换记录");
        mTitleBar.setRightIconAndText(R.mipmap.down_jiantou, year + "年", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time = mTitleBar.tvRight.getText().toString();
                if(!TextUtils.isEmpty(time)){
                    time.replace("年","");
                    SimpleDateFormat format = new SimpleDateFormat("yyyy");
                    try {
                        Date newTime = format.parse(time);
                        Calendar calendar1=    Calendar.getInstance();
                        calendar1.setTime(newTime);
                        pvTime.setDate(calendar1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                pvTime.show();
            }
        });


        tv_gotochange = findViewById(R.id.tv_gotochange);
        ImmersionBarUtils.initBaseBar(this);
        mStlContent = findViewById(R.id.stl_content);
        mViewpager = findViewById(R.id.viewpager);
        String[] titleList = new String[2];
        titleList[0] = "身份信息";
//        titleList[1] = "身份转换记录";
//        fragments.add(usertTypeListFregment);
        fragments.add(usertTypeHistoryFregment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), fragments);
        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mViewpager.setOffscreenPageLimit(2);
        mViewpager.setAdapter(homeTabPagerAdapter);
        mStlContent.setViewPager(mViewpager);
        mViewpager.setCurrentItem(0);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        month = Calendar.getInstance().get(Calendar.MONTH);
        tv_gotochange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.equals(userType, "电网公司代理用户")) {
                    if (month == 3 || month == 6 || month == 9 || month == 12) {
                        ARouter.getInstance()
                                .build(HomeArouterPath.HOME_USERTYPECHANGE)
                                .withInt("year", year)
                                .withInt("month", month)
//                                .withString("yearAndMonth", getNowTime())
                                .navigation();
                    } else {
                        ToastUtils.showShort("当前月份不能进行转换，下一个转换窗口期为20XX-XX月");
                    }
                } else {
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_USERTYPECHANGE)
                            .navigation();
                }
            }
        });

        initTimeSelect();
//        tv_time.setText(year + "");
//        tv_time.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                pvTime.show();
//            }
//        });
    }

    @Override
    protected void initData() {
        super.initData();

    }

    private void initTimeSelect() {
        //正确设置方式 原因：注意事项有说明
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        // 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        startDate.set(2021, month, 1);
        endDate.set(2099, 11, 1);
        //时间选择器
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = TimeUtils.date2String(date, " yyyy");
                selecttime = time.replace(" ", "");
                mTitleBar.tvRight.setText(time+"年");
                usertTypeHistoryFregment.setTime(selecttime);
//                usertTypeListFregment.setTime(selecttime);

            }
        }).setType(new boolean[]{true, false, false, false, false, false})
                .setDate(getDate())
                .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content))
                .setRangDate(startDate, endDate)
                .build();

    }

    private Calendar getDate() {
        return Calendar.getInstance();
    }

    private String getNowTime() {
        Date d = new Date();//获取时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//转换格式
        return sdf.format(d);
    }
}